package team.scau.laboratory_repair.common.activiti.listener;

import com.querydsl.core.QueryResults;
import lombok.extern.apachecommons.CommonsLog;
import org.activiti.engine.delegate.DelegateTask;
import org.activiti.engine.delegate.TaskListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import team.scau.laboratory_repair.common.constant.ConstantRole;
import team.scau.laboratory_repair.common.enums.RecordStatus;
import team.scau.laboratory_repair.common.message.MessageUtil;
import team.scau.laboratory_repair.modules.po.User;
import team.scau.laboratory_repair.modules.po.WechatUser;
import team.scau.laboratory_repair.service.*;
import team.scau.laboratory_repair.common.quartz.jobs.repair.CheckJob;
import team.scau.laboratory_repair.common.quartz.jobs.repair.MendJob;
import team.scau.laboratory_repair.common.quartz.jobs.repair.DelegateJob;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author 30254
 * createDate 2018/10/14
 */
//@Component
@CommonsLog
public class LabRepairHandler implements TaskListener {

    @Autowired
    private WechatUserService wechatUserService;

    @Autowired
    private JobService jobService;

    @Autowired
    private ActivitiService activitiService;

    @Autowired
    private RecordService recordService;

    private static final String MESSAGE_TIP = "快登陆微信端网页看看";

    private static final String CHECK_CRON = "* * */2 * * ?";

    @Override
    public void notify(DelegateTask delegateTask) {
        String name = delegateTask.getName();
        switch (name){
            case "审核并指派":delegate(delegateTask);break;
            case "维修":mend(delegateTask);break;
            case "确认":check(delegateTask);break;
            default:
        }
    }

    private void delegate(DelegateTask delegateTask){
        List<WechatUser> userList = wechatUserService.listManager();
        List<String> ids = new ArrayList<>();
        for (WechatUser u : userList) {
            ids.add(u.getId().toString());
        }
        delegateTask.addCandidateUsers(ids);
        for (WechatUser u : userList){
//            MessageUtil.sendMessageNewItem(u.getPhoneNumber(), RecordStatus.ORIGIN.toString(), MESSAGE_TIP);
        }
//        HashMap<String, Object> dataMap = new HashMap<>();
//        dataMap.put("taskId", delegateTask.getId());
//        try {
//            jobService.addJob(delegateTask.getId(), DelegateJob.class, CHECK_CRON, dataMap);
//        } catch (Exception e) {
//            log.error(e.getMessage(), e);
//        }
    }

    private void mend(DelegateTask delegateTask){
        String staffId = delegateTask.getVariable("staffId", String.class);
        delegateTask.setAssignee(staffId);
        WechatUser user = wechatUserService.findById(Long.valueOf(staffId));
//        MessageUtil.sendMessageNewItem(user.getPassword(),RecordStatus.DEALING.toString(), MESSAGE_TIP);
//        HashMap<String, Object> dataMap = new HashMap<>();
//        dataMap.put("taskId", delegateTask.getId());
//        try {
//            jobService.addJob(delegateTask.getId(), MendJob.class, CHECK_CRON, dataMap);
//        } catch (Exception e) {
//            log.error(e.getMessage(), e);
//        }
    }

    private void check(DelegateTask delegateTask){
        String managerId = delegateTask.getVariable("managerId", String.class);
        delegateTask.setAssignee(managerId);
        WechatUser user = wechatUserService.findById(Long.valueOf(managerId));
//        MessageUtil.sendMessageNewItem(user.getPassword(),RecordStatus.MENDED.toString(), MESSAGE_TIP);
//        HashMap<String, Object> dataMap = new HashMap<>();
//        dataMap.put("taskId", delegateTask.getId());
//        try {
//            jobService.addJob(delegateTask.getId(), CheckJob.class, CHECK_CRON, dataMap);
//        } catch (Exception e) {
//            log.error(e.getMessage(), e);
//        }
    }
}
